package servlet;

import com.alibaba.fastjson.JSON;
import entity.Books;
import service.BooksService;
import service.impl.BooksServiceImpl;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.io.PrintWriter;
import java.sql.ResultSet;
import java.text.SimpleDateFormat;
import java.util.*;
/**解决json数据{$ref: “$[0]“}问题*/
import com.alibaba.fastjson.serializer.SerializerFeature;

@WebServlet("/BooksServlet")
public class BooksServlet extends HttpServlet {
    @Override
    protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        req.setCharacterEncoding("utf-8");
        resp.setCharacterEncoding("utf-8");
        resp.setContentType("text/html;charset=UTF-8");
        PrintWriter out = resp.getWriter();
        BooksService booksService = new BooksServiceImpl();
        String opr = req.getParameter("opr");
        List<Books> booksList = new ArrayList<>();
        if (opr.equals("list")){
            String time = "";
            if(req.getParameter("time") != null){
                time = req.getParameter("time");
            }
            int uid = Integer.parseInt(req.getParameter("uid"));
            ResultSet rs = booksService.selectBooksList(uid,time);
            Books books;
            try{
                while (rs.next()){
                    books = new Books();
                    int bid = Integer.parseInt(rs.getString("bid"));
                    uid = Integer.parseInt(rs.getString("uid"));
                    int sid = Integer.parseInt(rs.getString("sid"));
                    int aid = Integer.parseInt(rs.getString("aid"));
                    int wid = Integer.parseInt(rs.getString("wid"));
                    double amount = Double.parseDouble(rs.getString("amount"));
                    String remark = rs.getString("remark");
                    time = rs.getString("time");
                    String url = rs.getString("url");
                    String approach = rs.getString("approach");
                    String status = rs.getString("status");
                    String uname = rs.getString("uname");
                    String wtname = rs.getString("wtname");
                    String wname = rs.getString("wname");

                    books.setBid(bid);
                    books.setUid(uid);
                    books.setSid(sid);
                    books.setAid(aid);
                    books.setWid(wid);
                    books.setAmount(amount);
                    books.setRemark(remark);
                    books.setTime(time);
                    books.setUrl(url);
                    books.setApproach(approach);
                    books.setStatus(status);
                    books.setUname(uname);
                    books.setWtname(wtname);
                    books.setWname(wname);

                    booksList.add(books);
                }
            }catch (Exception e){
                e.printStackTrace();
            }
            String infoListJson = JSON.toJSONString(booksList);
            out.print(infoListJson);
        }else if (opr.equals("add")){
            SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");//设置日期格式

            String time = dateFormat.format(new Date());// new Date()为获取当前系统时间，也可使用当前时间戳
            if(req.getParameter("time") != null){
                time = req.getParameter("time");
            }

            int uid = Integer.parseInt(req.getParameter("uid"));
            int sid = Integer.parseInt(req.getParameter("sid"));
            int aid = Integer.parseInt(req.getParameter("aid"));
            int wid = Integer.parseInt(req.getParameter("wid"));
            double amount = Double.parseDouble(req.getParameter("amount"));
            String remark = req.getParameter("remark");

            Books books = new Books();

            if (sid == 1){
                amount = 0 - amount;
            }

            books.setUid(uid);
            books.setSid(sid);
            books.setAid(aid);
            books.setWid(wid);
            books.setAmount(amount);
            books.setRemark(remark);
            books.setTime(time);

            int result = booksService.insertBooks(books);
            out.print(result);
        }else if (opr.equals("upd")){
            int sid = Integer.parseInt(req.getParameter("sid"));
            int aid = Integer.parseInt(req.getParameter("aid"));
            int wid = Integer.parseInt(req.getParameter("wid"));
            double amount = Double.parseDouble(req.getParameter("amount"));
            String remark = req.getParameter("remark");
            int bid = Integer.parseInt(req.getParameter("bid"));

            Books books = new Books();

            if (sid == 1){
                if (amount > 0){
                    amount = 0 - amount;
                }
            }else {
                if (amount < 0){
                    amount = 0 - amount;
                }
            }

            books.setSid(sid);
            books.setBid(bid);
            books.setAid(aid);
            books.setWid(wid);
            books.setAmount(amount);
            books.setRemark(remark);

            int result = booksService.updateBooks(books);
            out.print(result);
        }else if (opr.equals("del")){
            int bid = Integer.parseInt(req.getParameter("bid"));
            int r = booksService.deleteBooks(bid);
            out.print(r);
        }else if (opr.equals("books")){
            Map booksMap = new HashMap();
            int bid = Integer.parseInt(req.getParameter("bid"));
            ResultSet rs = booksService.selectBooks(bid);
            Books books;
            try{
                while (rs.next()){
                    books = new Books();
                    bid = Integer.parseInt(rs.getString("bid"));
                    int uid = Integer.parseInt(rs.getString("uid"));
                    int sid = Integer.parseInt(rs.getString("sid"));
                    int aid = Integer.parseInt(rs.getString("aid"));
                    int wid = Integer.parseInt(rs.getString("wid"));
                    double amount = Double.parseDouble(rs.getString("amount"));
                    String remark = rs.getString("remark");
                    String time = rs.getString("time");

                    books.setBid(bid);
                    books.setUid(uid);
                    books.setSid(sid);
                    books.setAid(aid);
                    books.setWid(wid);
                    books.setAmount(amount);
                    books.setRemark(remark);
                    books.setTime(time);

                    booksMap.put("books",books);
                }
            }catch (Exception e){
                e.printStackTrace();
            }
            String booksMapJson = JSON.toJSONString(booksMap);
            out.print(booksMapJson);
        }else if (opr.equals("amount")){
            int uid = Integer.parseInt(req.getParameter("uid"));
            int sid = Integer.parseInt(req.getParameter("sid"));
            String time = req.getParameter("time");


            double amount = 0;
            ResultSet rs = booksService.selectAmount(uid,sid,time);
            try {
                while (rs.next()){
                    amount = rs.getDouble(1);
                }
            }catch (Exception e){
                e.printStackTrace();
            }
            out.print(amount);
        }else if (opr.equals("currentMonthAmount")){
            int uid = Integer.parseInt(req.getParameter("uid"));


            double pay = 0;
            double income = 0;
            ResultSet rs = booksService.selectCurrentMonthAmount(uid);
            try {
                while (rs.next()){
                    if (rs.getInt("sid") == 1){
                        pay = rs.getDouble("amount");
                    }
                    if (rs.getInt("sid") == 2){
                        income = rs.getDouble("amount");
                    }
                }
            }catch (Exception e){
                e.printStackTrace();
            }
            Map data = new HashMap();
            data.put("pay",pay);
            data.put("income",income);
            String dataJSON = JSON.toJSONString(data);
            out.print(dataJSON);
        }else if (opr.equals("statistics")){
            int uid = Integer.parseInt(req.getParameter("uid"));
            int sid = Integer.parseInt(req.getParameter("sid"));


            int aid = 0;
            String approach = "";
            double amount = 0;

            List amountList = new ArrayList();

            ResultSet rs = booksService.selectSumAmount(uid,sid);
            try {
                while (rs.next()){
                    Map amountMap = new HashMap();

                    aid = rs.getInt("aid");
                    approach = rs.getString("approach");
                    amount = rs.getDouble("amount");

                    amountMap.put("aid",aid);
                    amountMap.put("approach",approach);
                    amountMap.put("amount",amount);


                    amountList.add(amountMap);
                }
            }catch (Exception e){
                e.printStackTrace();
            }

            System.out.println(amountList);
            String dataJSON = JSON.toJSONString(amountList,SerializerFeature.DisableCircularReferenceDetect);
            out.print(dataJSON);
        }

//        req.setAttribute("booksList",booksList);
//        req.getRequestDispatcher("books.jsp").forward(req,resp);
    }

    @Override
    protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        this.doGet(req, resp);
    }
}
